﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace BookNet
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Label1.Visible = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            // Validate the user against the Membership framework user store
            if (Membership.ValidateUser(Username.Text, Password.Text))
            {
                // Log the user into the site
                FormsAuthentication.RedirectFromLoginPage(Username.Text, true);
            }
            // If we reach here, the user's credentials were invalid
            Label1.Visible = true;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Label1.Visible = false;
            MembershipCreateStatus createStatus;
            MembershipUser newUser = Membership.CreateUser(NewUserName.Text, NewPassword.Text, Email.Text, SecurityQuestion.Text, Answer.Text, true, out createStatus);
            if (createStatus == MembershipCreateStatus.Success)
            {
                MembershipUser NewUser = Membership.GetUser(NewUserName.Text);
                Guid newUserId = (Guid)newUser.ProviderUserKey;
                // Insert a new record into UserProfiles
                string connectionString = ConfigurationManager.ConnectionStrings["SecurityConnectionString"].ConnectionString;
                string insertSql = "INSERT INTO app_UserProfile(UserId, FullName, Gender, Books, DateofBirth, Friends, Favorites, Faculty, Bio) VALUES(@UserId, @FullName, @Gender, @Books, @DateofBirth, @Friends, @Favorites, @Faculty, @Bio)";
                using (SqlConnection myConnection = new SqlConnection(connectionString))
                {
                    myConnection.Open();
                    SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
                    myCommand.Parameters.AddWithValue("@UserId", newUserId);
                    myCommand.Parameters.AddWithValue("@FullName", DBNull.Value);
                    myCommand.Parameters.AddWithValue("@Gender", 0);
                    myCommand.Parameters.AddWithValue("@Books", DBNull.Value);
                    myCommand.Parameters.AddWithValue("@DateofBirth", DateTime.Now);
                    myCommand.Parameters.AddWithValue("@Friends", DBNull.Value);
                    myCommand.Parameters.AddWithValue("@Favorites", DBNull.Value);
                    myCommand.Parameters.AddWithValue("@Faculty", DBNull.Value);
                    myCommand.Parameters.AddWithValue("@Bio", DBNull.Value);
                    myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
                //System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                //msg.To.Add(Email.Text);
                //msg.Body = "fuck you!";
                //msg.IsBodyHtml = false;
                //System.Net.Mail.SmtpClient s = new System.Net.Mail.SmtpClient();
                //try
                //{
                //    s.Send(msg);
                //}
                //catch (Exception ex)
                //{
                //    Response.Write(ex);
                //}
            }
            switch (createStatus)
            {
                case MembershipCreateStatus.Success:
                    Label2.Text = "The user account was successfully created!";
                    break;
                case MembershipCreateStatus.DuplicateUserName:
                    Label2.Text = "There already exists a user with this username.";
                    break;
                case MembershipCreateStatus.DuplicateEmail:
                    Label2.Text = "There already exists a user with this email address.";
                    break;
                case MembershipCreateStatus.InvalidEmail:
                    Label2.Text = "There email address you provided in invalid.";
                    break;
                case MembershipCreateStatus.InvalidAnswer:
                    Label2.Text = "There security answer was invalid.";
                    break;
                case MembershipCreateStatus.InvalidPassword:
                    Label2.Text = "The password you provided is invalid. It must be seven characters long and have at least one non-alphanumeric character.";
                    break;
                default:
                    Label2.Text = "There was an unknown error; the user account was NOT created.";
                    break;
            }
        }
    }
}
